﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Interfaces.Services;
using Tateeda.ClinicalStudy.Base.Modules.Models.StudyVisit;
using Tateeda.ClinicalStudy.Base.Web.Common;

namespace Tateeda.ClinicalStudy.Base.Web.Controllers {

    public class VisitStepController : Controller {
        private IAdminService _service;

        public VisitStepController(IAdminService service) {
            _service = service;
        }

        // GET: /VisitStep/
        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult Index() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult Sequence() {
            var sequence = _service.GetAllVisitsSequences();
            return View(sequence);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult Steps() {
            return View(_service.GetAllVisitSteps());
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult EditStep(int id) {
            var steps = _service.GetAllVisitSteps();
            return View(steps.Where(sid => sid.VisitStepId == id).FirstOrDefault());
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        [HttpPost]
        public ActionResult EditStep(VisitStepModel step) {
            if (_service.UpdateVisitStep(step))
                return View();
            return View(step);
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult DeleteStep(int id) {
            _service.DeleteVisitStep(id);
            return RedirectToAction("Index");
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult CreateStep() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        [HttpPost]
        public ActionResult CreateStep(VisitStepModel step) {
            _service.CreateVisitStep(step);
            return RedirectToAction("Index");
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdminAppuser)]
        public ActionResult Mapping() {
            return View();
        }
    }
}